var path;
var cur;

$(document).ready(function(){

	path = $("#path").val();
	//$(".setDefault").live("click",setDefaultIDCard);
	$(".deleteIDCard").live("click",delIDCard);
	$(".submitAddress").live("click",saveIDCard);
});

function viewIDCard(){
	window.location.href=path+"/user/identitycard/view/0";
}
function saveIDCard(){
	if(!checkInput()){
		return;
	}
	$("body").find("form[name='idcard-form']").submit();
}
function delIDCard(){
	cur = $(this);
	var idCardId = getIDCardId();
	if(isEmpty(idCardId)){
		return;
	}
	
	$.dialog({
        content : '确认删除吗？',
        title : 'ok',
        ok : function() {
            var url = path + "/user/identitycard/remove/"+idCardId;
            $.ajax({
                type: "post",
                dataType: "text",
                async:true,
                url:url,
                success: function (result) {
                	if(result != "YYK-10000"){
                		$.dialog({
                			content : '系统繁忙，请稍候重试',
                			title : '温馨提示',
                			lock : true,
                			time : 2000
                		});
                	}else{
                		$.dialog({
                			content : '删除成功',
                			title : '温馨提示',
                			lock : true,
                			time : 1000
                		});
                		$(".contentWrap").load(path+"/user/identitycard/findAll #wrapper",function(responseText, textStatus, jqXHR){
        					if(textStatus == "success" || textStatus == "notmodified"){
        				        loaded();
        				    }
        				});
                	}
                }
        	 });
            
        },
        cancel : function() {
        },
        lock : true
    });
}

function getIDCardId(){
	return cur.parents(".idcardli").attr("id");
}

function setDefaultIDCard(){
	cur = $(this);
	var id = getIDCardId();

	if(isEmpty(id)){
		return;
	}
	
	var url = path + "/user/identitycard/isDefault/"+id;
	
	$.ajax({
        type: "post",
        dataType: "text",
        async:true,
        url:url,
        success: function (result) {
        	if(result != "YYK-10000"){
        		$.dialog({
        			content : '系统繁忙，请稍候重试',
        			title : '温馨提示',
        			lock : true,
        			time : 2000
        		});
        	}else{
        		$.dialog({
        			content : '设置成功',
        			title : '温馨提示',
        			lock : true,
        			time : 1000
        		});
        		$(".contentWrap").load(path+"/user/identitycard/findAll #wrapper",function(responseText, textStatus, jqXHR){
					if(textStatus == "success" || textStatus == "notmodified"){
				        loaded();
				    }
				});
        	}
        }
	 });
}




function checkInput(){
	
	var idcardForm = $("body").find("form[name='idcard-form']");
	// 收货人姓名   
	var idcardName = idcardForm.find("input[name='identityCardName']").val();
	if(isEmpty(idcardName)){
		$.dialog({
			content : '请填写身份证姓名',
			title : '温馨提示',
			lock : true,
			time : 1500
		});
		return false;
	}
	
	// 收货人电话
	var idcardNumber = idcardForm.find("input[name='identityCardNumber']").val();
	if(isEmpty(idcardNumber)){
		$.dialog({
			content : '请填写身份证号码',
			title : '温馨提示',
			lock : true,
			time : 1500
		});
		return false;
	}
	var regName = /^[\u4E00-\u9FA5]+$/;
	if(!regName.test(idcardName)){
		$.dialog({
			content : '请填写有效身份证姓名',
			title : '温馨提示',
			lock : true,
			time : 1500
		});
		return false;
	}
	if(!IdCardValidate(idcardNumber)){
		$.dialog({
			content : '请填写有效身份证号码',
			title : '温馨提示',
			lock : true,
			time : 1500
		});
		return false;
	}
	
	return true;
}
